package org.lanqiao.dao.Impl;

import org.lanqiao.dao.AdminDao;
import org.lanqiao.entity.Admin;
import org.lanqiao.util.DBUtil;

import java.util.List;

public class AdminDaoImpl implements AdminDao {

    @Override
    public int addAdmin(Admin admin) {
        String sql="insert into admin(username,password,name,address,birthday) values(?,?,?,?,?)";
        return DBUtil.executeUpdate(sql,admin.getUsername(),admin.getPassword(),admin.getName(),admin.getAddress(),admin.getBirthday());

    }

    @Override
    public int delAdmin(Integer id) {
        String sql="delete from admin where id=?";
        return DBUtil.executeUpdate(sql,id);
    }

    @Override
    public int updAdmin(Admin admin) {
        String sql="update admin set username=?,password=?,name=?,address=?,birthday=?where id=?";
        return DBUtil.executeUpdate(sql,admin.getUsername(),admin.getPassword(),admin.getName(),admin.getAddress(),admin.getBirthday(),admin.getId());

    }

    @Override
    public Admin findAdminById(Integer id) {
        String sql="select*from admin where id=?";
        List<Admin>list=DBUtil.executeQuery(Admin.class,sql,id);
        return list.isEmpty()?null:list.get(0);
    }

    @Override
    public List<Admin> findAllAdmins() {
        String sql="select*from admin";
        return DBUtil.executeQuery(Admin.class,sql);
    }

    @Override
    public boolean isExistById(Integer id) {
        Admin train =this.findAdminById(id);
        if (train==null){
            return false;
        }else {
            return true;
        }
    }
}